﻿namespace Wicresoft.Busi.BizLogic
{
    using System;
    using System.Data;
    using Wicresoft.UID.Organization.Entity;

    public class BLUser
    {
        public static Group GetParentDept(long UserID)
        {
            try
            {
                DataTable dt = Organization.GetSingleUser(UserID).GetMainRelationPath(Util.RootGroupID);
                int nIndex = dt.Rows.Count - Util.Deep;
                long NodeID = UserID;
                while (nIndex >= 0)
                {
                    dt.DefaultView.RowFilter = "NodeID = " + NodeID;
                    foreach (DataRowView rv in dt.DefaultView)
                    {
                        NodeID = long.Parse(rv["ParentNodeID"].ToString());
                    }
                    nIndex--;
                }
                return Organization.GetSingleGroup(NodeID);
            }
            catch
            {
                return null;
            }
        }

        public static bool IsInRole(long RoleID, long UserID)
        {
            foreach (DataRow dr in Organization.GetSingleUser(UserID).GetAncestorRole().Rows)
            {
                if (((long) dr["ParentNodeID"]) == RoleID)
                {
                    return true;
                }
            }
            return false;
        }
    }
}

